const fs = require('fs');
const nodeSchedule = require('node-schedule');
const request = require('request');

const newsType = [
  'www_www_all_suda_suda',
  'news_china_suda',
  'news_world_suda',
  'news_society_suda',
  'sports_suda',
  'finance_0_suda',
  'ent_suda',
  'tech_news_suda',
];

const newsTypeObj = {
  www_www_all_suda_suda: '简报',
  news_china_suda: '国内',
  news_world_suda: '国外',
  news_society_suda: '社会',
  sports_suda: '体育',
  finance_0_suda: '财经',
  ent_suda: '娱乐',
  tech_news_suda: '科技',
};

let promiseList = [];

const nowDate = new Date().toLocaleDateString().split('/').join('');

function getNews(type) {
  let domain;
  if (type !== 'www_www_all_suda_suda') {
    domain = type.split('_')[0];
  } else {
    domain = 'news';
  }
  const url = `https://top.${domain}.sina.com.cn/ws/GetTopDataList.php?top_type=day&top_cat=${type}&top_time=${nowDate}&top_show_num=10&top_order=DESC&js_var=data`;
  let promise = new Promise((resolve, reject) => {
    request(
      {
        url: url,
        method: 'GET',
        json: true,
        headers: {
          'content-type': 'application/json',
        },
      },
      function (error, response, body) {
        if (!error) {
          var dataStr = body.substr('var data = '.length);
          dataStr = dataStr.substring(0, dataStr.lastIndexOf(';'));
          var data = JSON.parse(dataStr).data;

          data.map((item) => {
            item.type = newsTypeObj[type];
          });
          resolve(data);
        } else {
          reject(error);
        }
      }
    );
  });
  return promise;
}

function setNews() {
  newsType.forEach((item) => {
    promiseList.push(getNews(item));
  });

  Promise.all(promiseList)
    .then((res) => {
      //将获取到的数据写入hotSearch.json中
      fs.writeFileSync(
        `${__dirname}/hotSearch.json`,
        JSON.stringify(res, null, 2),
        'utf-8'
      );
      //可以自行根据自身需求灵活运用请求到的所有新闻res
    })
    .catch((err) => {
      fs.writeFileSync(
        `${__dirname}/error.json`,
        JSON.stringify(err, null, 2),
        'utf-8'
      );
      console.error('报错产生，报错信息保存在error.json！');
    });
}

/*
 * schedule

*    *    *    *    *    *    
┬    ┬    ┬    ┬    ┬    ┬
│    │    │    │    │    │
│    │    │    │    │    └ day of week (0 - 7) (0 or 7 is Sun)
│    │    │    │    └───── month (1 - 12)
│    │    │    └────────── day of month (1 - 31)
│    │    └─────────────── hour (0 - 23)
│    └──────────────────── minute (0 - 59)
└───────────────────────── second (0 - 59, OPTIONAL)

 */
/**
 * 每分钟第30秒定时执行爬取任务
 */
nodeSchedule.scheduleJob('0 30 3 * * *', async function () {
  setNews();
});
